package cn.yr.system.controller;

import cn.hutool.core.util.StrUtil;
import cn.yr.common.core.constant.CommonConstants;
import cn.yr.common.core.constant.UserConstants;
import cn.yr.common.core.domain.entity.SysMenu;
import cn.yr.common.core.manager.AuthUserManager;
import cn.yr.common.exception.ret.Ret;
import cn.yr.common.security.annotation.PreAuthorize;
import cn.yr.system.service.ISysMenuService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * @author Lang 1102076808@qq.com
 * @description 菜单
 * @date 2020-07-10 20:44
 */
@RestController
@RequestMapping("/sys/menu")
public class SysMenuController{

    @Resource
    private ISysMenuService menuService;

	@Resource
	private AuthUserManager authUserManager;
	/**
	 * 获取路由信息
	 * @return 路由信息
	 */
	@GetMapping("getRouters")
	public Ret getRouters(){
		List<SysMenu> menus = menuService.selectMenuTreeByUserId(authUserManager.uid());
		return Ret.success(menuService.buildMenus(menus));
	}

	/**
	 * 获取菜单列表
	 */
    @PreAuthorize(hasPermi = "system:menu:list")
	@GetMapping("/list")
	public Ret list(SysMenu menu) {
		List<SysMenu> menus = menuService.selectMenuList(menu, authUserManager.uid());
		return Ret.success(menus);
	}

    /**
     * 根据菜单编号获取详细信息
     */
    @PreAuthorize(hasPermi = "system:menu:query")
    @GetMapping(value = "/{menuId}")
    public Ret getInfo(@PathVariable Long menuId) {
        return Ret.success(menuService.selectMenuById(menuId));
    }

    /**
     * 获取菜单下拉树列表
     */
    @GetMapping("/treeselect")
    @PreAuthorize(hasPermi = "system:menu:list")
    public Ret treeselect(SysMenu menu) {
        List<SysMenu> menus = menuService.selectMenuList(menu, authUserManager.uid());
        return Ret.success(menuService.buildMenuTreeSelect(menus));
    }

    /**
     * 加载对应角色菜单列表树
     */
    @PreAuthorize(hasPermi = "system:menu:list")
    @GetMapping(value = "/roleMenuTreeselect/{roleId}")
    public Ret roleMenuTreeselect(@PathVariable("roleId") Long roleId) {
        List<SysMenu> menus = menuService.selectMenuList(authUserManager.uid());
        HashMap<String, Object> map = new HashMap<>();
        map.put("checkedKeys", menuService.selectMenuListByRoleId(roleId));
        map.put("menus", menuService.buildMenuTreeSelect(menus));
        return Ret.success(map);
    }

    /**
     * 新增菜单
     */
    @PreAuthorize(hasPermi = "system:menu:add")
    @PostMapping("/add")
    public Ret add(@Validated @RequestBody SysMenu menu) {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return Ret.error("新增菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
                && !StrUtil.startWithAny(menu.getPath(), CommonConstants.HTTP, CommonConstants.HTTPS)) {
            return Ret.error("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        return Ret.success(menuService.insertMenu(menu));
    }

    /**
     * 修改菜单
     */
    @PreAuthorize(hasPermi = "system:menu:edit")
    @RequestMapping("/edit")
    public Ret edit(@Validated @RequestBody SysMenu menu) {
        if (UserConstants.NOT_UNIQUE.equals(menuService.checkMenuNameUnique(menu))) {
            return Ret.error("修改菜单'" + menu.getMenuName() + "'失败，菜单名称已存在");
        } else if (UserConstants.YES_FRAME.equals(menu.getIsFrame())
                && !StrUtil.startWithAny(menu.getPath(), CommonConstants.HTTP, CommonConstants.HTTPS)) {
            return Ret.error("新增菜单'" + menu.getMenuName() + "'失败，地址必须以http(s)://开头");
        }
        return Ret.success(menuService.updateMenu(menu));
    }

    /**
     * 删除菜单
     */
    @PreAuthorize(hasPermi = "system:menu:remove")
    @RequestMapping("/remove/{menuId}")
    public Ret remove(@PathVariable("menuId") Long menuId) {
        if (menuService.hasChildByMenuId(menuId)) {
            return Ret.error("存在子菜单,不允许删除");
        }
        if (menuService.checkMenuExistRole(menuId)) {
            return Ret.error("菜单已分配,不允许删除");
        }
        return Ret.success(menuService.deleteMenuById(menuId));
    }

}